package com.youku.plugins.commonui.view
{
   import flash.display.MovieClip;
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.plugins.commonui.view.noticeCard.BaseNoticeCard;
   import com.youku.ExternalProxy;
   import com.youku.GlobalService;
   import com.youku.GlobalServiceKeys;
   import com.youku.events.PlayerControlEvent;
   import com.youku.plugins.commonui.CommonUIEvent;
   import com.youku.plugins.commonui.view.noticeCard.ErrorCard;
   import com.youku.plugins.commonui.view.noticeCard.NoticeCard;
   import com.youku.plugins.commonui.view.noticeCard.InputPasswordCard;
   import com.youku.plugins.commonui.view.noticeCard.ErrorChangeBrowserCard;
   import com.youku.plugins.commonui.view.noticeCard.PayCardLoader;
   import com.youku.error.ErrorType;
   import com.youku.events.PluginEvent;
   import com.youku.PlayerConfig;
   import flash.net.URLRequest;
   import com.youku.utils.OpenWindow;
   
   public class NoticeCardMediator extends Object
   {
      
      public function NoticeCardMediator(param1:MovieClip, param2:IPlayerProxy) {
         super();
         this._mainMc = param1;
         this._playerProxy = param2;
         this.Init();
      }
      
      private var _mainMc:MovieClip;
      
      private var _playerProxy:IPlayerProxy;
      
      private var _noticeCard:BaseNoticeCard;
      
      private var _width:Number;
      
      private var _height:Number;
      
      private var GET_FLASH_LINK:String = "http://www.adobe.com/go/getflash";
      
      private function get externalProxy() : ExternalProxy {
         return GlobalService.getService(GlobalServiceKeys.EXTERNALINTERFACE_PROXY) as ExternalProxy;
      }
      
      private function Init() : void {
         this._width = 200;
         this._height = 200;
         this._playerProxy.addEventListener(PlayerControlEvent.SHOW_ERROR_CARD,this.onShowErrorCard);
         this._playerProxy.addEventListener(PlayerControlEvent.PLAYER_SIZE_CHANGED,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.FULLSCREEN_CHANGED,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_RESET,this.onPlayerControlEvent);
      }
      
      private function attchEvents(param1:Boolean) : void {
         if(this._noticeCard == null)
         {
            return;
         }
         if(param1)
         {
            this._noticeCard.addEventListener(CommonUIEvent.NOTICE_PASSWORD_CONFIRM,this.onErrorCardEvent);
            this._noticeCard.addEventListener(CommonUIEvent.NOTICE_PASSWORD_CONTACT,this.onErrorCardEvent);
            this._noticeCard.addEventListener(CommonUIEvent.NOTICE_PAY_BUY,this.onErrorCardEvent);
            this._noticeCard.addEventListener(CommonUIEvent.NOTICE_PAY_LOGIN,this.onErrorCardEvent);
            this._noticeCard.addEventListener(CommonUIEvent.ERROR_CARD_CONFIRM_CLICK,this.onErrorCardEvent);
            this._noticeCard.addEventListener(CommonUIEvent.NOTICE_REFRESH,this.onErrorCardEvent);
            this._noticeCard.addEventListener(CommonUIEvent.NOTICE_BUG_REPORT,this.onErrorCardEvent);
            this._noticeCard.addEventListener(CommonUIEvent.NOTICE_GET_FLASH,this.onErrorCardEvent);
         }
         else
         {
            this._noticeCard.removeEventListener(CommonUIEvent.NOTICE_PASSWORD_CONFIRM,this.onErrorCardEvent);
            this._noticeCard.removeEventListener(CommonUIEvent.NOTICE_PASSWORD_CONTACT,this.onErrorCardEvent);
            this._noticeCard.removeEventListener(CommonUIEvent.NOTICE_PAY_BUY,this.onErrorCardEvent);
            this._noticeCard.removeEventListener(CommonUIEvent.NOTICE_PAY_LOGIN,this.onErrorCardEvent);
            this._noticeCard.removeEventListener(CommonUIEvent.ERROR_CARD_CONFIRM_CLICK,this.onErrorCardEvent);
            this._noticeCard.removeEventListener(CommonUIEvent.NOTICE_REFRESH,this.onErrorCardEvent);
            this._noticeCard.removeEventListener(CommonUIEvent.NOTICE_BUG_REPORT,this.onErrorCardEvent);
            this._noticeCard.removeEventListener(CommonUIEvent.NOTICE_GET_FLASH,this.onErrorCardEvent);
         }
      }
      
      private function onShowErrorCard(param1:PlayerControlEvent) : void {
         var _loc5_:String = null;
         var _loc6_:String = null;
         var _loc2_:String = param1.data.type;
         var _loc3_:String = param1.data.message;
         var _loc4_:String = param1.data.code;
         _loc3_ = _loc3_.replace("<a href=\"asfunction:_parent.playerAPI.refreshplayer\">","<a href=\"event:refresh\">");
         this.attchEvents(false);
         if(!(this._noticeCard == null) && (this._mainMc.contains(this._noticeCard)))
         {
            this._mainMc.removeChild(this._noticeCard);
         }
         switch(_loc2_)
         {
            case ErrorType.NORMAL_ERROR:
               this._noticeCard = new ErrorCard();
               break;
            case ErrorType.NORMAL_NOTICE:
               this._noticeCard = new NoticeCard();
               _loc4_ = "";
               break;
            case ErrorType.NEED_PASSWORD:
               this._noticeCard = new InputPasswordCard();
               break;
            case ErrorType.CHANGE_BROWSER_ERROR:
               this._noticeCard = new ErrorChangeBrowserCard();
               break;
            case ErrorType.PAY_LOGINED_BUY:
            case ErrorType.PAY_NOT_LOGIN:
            case ErrorType.PAY_LIMIT_COUNT:
            case ErrorType.PAY_OUT_OF_DATE:
            case ErrorType.PAY_TRIAL_END_LOGINED:
            case ErrorType.PAY_TRIAL_END_NOT_LOGIN:
               _loc5_ = String(Math.round(new Date().getTime() / (1000 * 60 * 60)));
               _loc6_ = "http://v.youku.com/player/getTrySwf?showid=" + this._playerProxy.playerData.videoShow.showID + "&randoms=" + _loc5_;
               this._noticeCard = new PayCardLoader();
               this._noticeCard.load(_loc6_);
               break;
         }
         this._noticeCard.setErrorCode(_loc4_);
         this.attchEvents(true);
         this._mainMc.addChild(this._noticeCard);
         this._noticeCard.setSize(this._width,this._height);
         if(_loc3_ != null)
         {
            this._noticeCard.setText(_loc3_);
         }
      }
      
      private function onPlayerControlEvent(param1:PlayerControlEvent) : void {
         var _loc2_:* = NaN;
         var _loc3_:* = NaN;
         var _loc4_:* = NaN;
         var _loc5_:* = false;
         switch(param1.type)
         {
            case PlayerControlEvent.FULLSCREEN_CHANGED:
            case PlayerControlEvent.PLAYER_SIZE_CHANGED:
               _loc2_ = param1.data.width;
               _loc3_ = param1.data.height;
               _loc4_ = param1.data.cheight;
               _loc5_ = param1.data.fullscreen;
               this._width = _loc2_;
               this._height = _loc3_ - (_loc5_?0:_loc4_);
               if(this._noticeCard != null)
               {
                  this._noticeCard.setSize(this._width,this._height);
               }
               break;
            case PlayerControlEvent.COMMON_RESET:
               this.hideErrorCard();
               break;
         }
      }
      
      private function onErrorCardEvent(param1:CommonUIEvent) : void {
         var _loc2_:String = null;
         var _loc3_:String = null;
         switch(param1.type)
         {
            case CommonUIEvent.ERROR_CARD_CONFIRM_CLICK:
               this.hideErrorCard();
               break;
            case CommonUIEvent.NOTICE_PASSWORD_CONFIRM:
               _loc2_ = param1.data as String;
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.ERRORCARD_PLAY_WITH_PASSWORD,_loc2_));
               this.hideErrorCard();
               break;
            case CommonUIEvent.NOTICE_PASSWORD_CONTACT:
               this.jumpToUserPage();
               break;
            case CommonUIEvent.NOTICE_PAY_BUY:
               this.jumpToLink();
               break;
            case CommonUIEvent.NOTICE_PAY_LOGIN:
               this.jumpToLink();
               break;
            case CommonUIEvent.NOTICE_REFRESH:
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.ERRORCARD_REFRESH));
               break;
            case CommonUIEvent.NOTICE_BUG_REPORT:
               _loc3_ = param1.data as String;
               this.jumpToBugReportPage(_loc3_);
               break;
            case CommonUIEvent.NOTICE_GET_FLASH:
               this.jumpToGetFlashLink();
               break;
         }
      }
      
      private function jumpToUserPage() : void {
         var _loc1_:String = String(PlayerConfig.playListData.userId);
         var _loc2_:String = "http://u.youku.com/user_show/id_" + _loc1_;
         var _loc3_:URLRequest = new URLRequest(_loc2_);
         OpenWindow.open(_loc3_,"_blank");
      }
      
      private function jumpToLink() : void {
         if((this._playerProxy.playerData.trialInfo) && (this._playerProxy.rootGetData.rootTrialJS))
         {
            this.externalProxy.onTrialEnd();
         }
         else
         {
            this.externalProxy.onTrialLinkClick();
         }
      }
      
      private function jumpToBugReportPage(param1:String) : void {
         var _loc2_:* = "http://www.youku.com/service/feed/subtype/2/";
         var _loc3_:String = this._playerProxy.playerData.videoIdEncode;
         if(_loc3_ == null || _loc3_ == "")
         {
            _loc3_ = this._playerProxy.rootGetData.rootVideoIdEncode;
         }
         _loc2_ = _loc2_ + ("?code=" + param1);
         _loc2_ = _loc2_ + ("&videoID=" + _loc3_);
         OpenWindow.openURL(_loc2_);
      }
      
      private function jumpToGetFlashLink() : void {
         OpenWindow.openURL(this.GET_FLASH_LINK);
      }
      
      private function hideErrorCard() : void {
         this.attchEvents(false);
         if(!(this._noticeCard == null) && (this._mainMc.contains(this._noticeCard)))
         {
            this._mainMc.removeChild(this._noticeCard);
         }
         this._noticeCard = null;
      }
   }
}
